package com.gzsxy.JUC;

/**
 * synchronized
 */
public class SaleTickDemo {

    public static void main(String[] args) {
        //并发：多个线程操作同一个资源类,把资源丢入到线程
        Ticket ticket = new Ticket();

        //匿名内部类直接new 接口Runable   最后简化为（）
        new Thread(() -> {

            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }


        }, "A").start();


        new Thread(() -> {

            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "B").start();


        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "C").start();
    }


}

class Ticket {
    private int number = 50;

    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了" + (number--) + "票" + "剩余" + number);
        }
    }
}